// pages/red-packet-help/help.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    rid:'',
    userid:'',
    token:'',
    couponlist:'',
    toke:'',
    tupian:'',
    yonghuid:'',
    islogin: false,
    succeed:0,
    province: '',
    city: '',
    district: '',
  },
  // 先获取数据
  getlist:function(){
    console.log(1)
    console.log(this.data.id)
    console.log(this.data.rid)
    console.log(wx.getStorageSync('PersonID'),123)
    console.log(wx.getStorageSync('Access_token'),123)
    ajax('/api/ZLMDShare/getCBLQ?requestContent=' + JSON.stringify({
      token: this.data.token,
      ID: this.data.id,
      ReceiveCouponID: this.data.rid,
      PersonID: this.data.userid,
      Province: this.data.province,
      City: this.data.city,
      District: this.data.district
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {
        console.log(res,'778899')
        this.setData({
          couponlist: res.Models
        })
      })
  },
  //  授权
  bindGetUserInfo(e) {
    console.log(e)
    var that = this;
    console.log(e.detail.userInfo, 123456789)
    if (e.detail.userInfo) {
      // 用户按了允许授权按钮
      var code = wx.getStorageSync("code");
      ajax('/api/WXUserLogin/Insert?requestContent=' + JSON.stringify({
        code: code,
        nickname: e.detail.userInfo.nickName,
        avatar: e.detail.userInfo.avatarUrl,
        UserType: 3
      }), {
          header: {
            'Accept': 'application/x-www-form-urlencoded', // 默认值
          },
          method: "post",
        }, res => {
          console.log(res,'998877')
          // wx.setStorageSync("Access_tokenone", res.Access_token)
          // wx.setStorageSync("HeadImg", e.detail.userInfo.avatarUrl)
          // wx.setStorageSync("CurunionID", res.SignID)
          this.setData({
            islogin: true,
            toke: res.Access_token,
            tupian: e.detail.userInfo.avatarUrl,
            yonghuid: res.SignID
          })
          console.log(this.data.toke)
          console.log(this.data.tupian)
          console.log(this.data.yonghuid)
          // console.log(this.data.toke)
          if (this.data.yonghuid == this.data.userid){
            this.setData({
              succeed: 2
            })
            this.getlist()
          }
          
        })
    } else {
      wx.showToast({
        title: '请先授权！',
        icon: 'none',
        duration: 2000
      })

    }
  },
  bangcha:function(){
    ajax('/api/ZLMDShare/AddCBLQ?requestContent=' + JSON.stringify({
      token: this.data.toke,
      CurunionID: this.data.yonghuid,
      HeadImg: this.data.tupian,
      ReceiveCouponID: this.data.rid,
      PersonID: this.data.userid
      
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "POST",
      }, res => {
        console.log(res,'磅差')
        this.setData({
          succeed:1
        })
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,'889977')
    var id = options.id
    var rid = options.ReceiveCouponID
    var userid = options.PersonID
    var token=options.token
    var province=options.province
    var city=options.city
    var district=options.district
    this.setData({
      id: id,
      rid: rid,
      userid: userid,
      token:token,
      province:options.province,
      city : options.city,
      district : options.district
    })
    // this.getlist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getlist()
    // this.bindGetUserInfo()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})